package com.yufan.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * mysql数据源
 */
@Configuration
@MapperScan(basePackages = MysqlConfigBase.Package, sqlSessionFactoryRef = MysqlConfigBase.sessionFactory)
public class MysqlConfigBase {

    // --------------------------------- customer config -------------------------------
    private static final String dbName = "base";//数据源配置后缀(需对应配置修改)
    private static final String config = "spring.datasource.base";
    static final String Package = "com.gis.study.dao.base";//mapper目录(需对目录修改)
    private static String mybatisMapperLocations = "";//classpath:mapper/*.xml
    // ---------------------------------------------------------------------------------


    private static final String dataSource = dbName + "DataSource";
    private static final String transactionManager = dbName + "TransactionManager";
    static final String sessionFactory = dbName + "SqlSessionFactory";
    static final String sessionTemplate = dbName + "SqlSessionTemplate";


    @Bean(dataSource)
    @ConfigurationProperties(prefix = config)
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }


    @Bean(transactionManager)
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean(sessionFactory)
    public SqlSessionFactory sessionFactory(@Qualifier(dataSource) DataSource mysqlDataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(mysqlDataSource);
        if (mybatisMapperLocations != null && mybatisMapperLocations.length() > 0) {
            sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mybatisMapperLocations));
        }

        //mybatis config
        org.apache.ibatis.session.Configuration configuration=new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);//驼峰
        sessionFactory.setConfiguration(configuration);

        return sessionFactory.getObject();
    }

    @Bean(sessionTemplate)
    public SqlSessionTemplate sessionTemplate(@Qualifier(sessionFactory) SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}